const puppeteer = require('puppeteer');

const sleep = function(time) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve();
    }, time);
  });
};

const test = async () => {

  const defaultViewport = {
    deviceScaleFactor: 1,
    hasTouch: false,
    height: 1024,
    isLandscape: false,
    isMobile: false,
    width: 1280,
  };

  const browser = await puppeteer.launch({
    args: [
      // '--disable-background-networking',
      // '--disable-default-apps',
      '--disable-extensions-except=/home/liusen/WebStormWorkSpace/jd-task/extension/',
      '--load-extension=/home/liusen/WebStormWorkSpace/jd-task/extension/',
      '--disable-sync',
      '--disable-translate',
      // '--hide-scrollbars',
      '--metrics-recording-only',
      '--mute-audio',
      '--no-first-run',
      '--safebrowsing-disable-auto-update',
      // '--ignore-certificate-errors',
      // '--ignore-certificate-errors-spki-list',
      '--user-data-dir=/tmp',
      '--remote-debugging-port=9222',
      '--remote-debugging-address=0.0.0.0',
    ],
    executablePath: '/usr/bin/google-chrome-stable',
    headless: false,
  });

  // const browser = await puppeteer.connect(
  //     {browserWSEndpoint: 'ws://localhost:3000'});

  console.log(`browser URL = ${browser._connection.url()}`);
  // const page = await browser.newPage();
  // await page.setViewport(defaultViewport);
  // await page.goto('https://coin.jd.com/asset/sc/myassets.html');
  require('fs').
    writeFileSync('./endpoint.json',
      JSON.stringify({browserWSEndpoint: browser._connection.url()}));
  // await sleep(100000 * 1000);
  // await browser.close();
};

test().then(() => {}).catch(err => {
  console.log(err);
});
